/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * EntregaMaterialFrame.java
 *
 * Created on 11/07/2011, 09:27:16 PM
 */
package jacidco;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;

/**
 *
 * @author Sefer Sephirott
 */
public class EntregaMaterialFrame extends javax.swing.JFrame implements BotonesNuevoCrearCancelar
{
    private int codAfiliado;
    private int codMaterial;
    private String estado;

    /** Creates new form EntregaMaterialFrame */
    public EntregaMaterialFrame() 
    {
        initComponents();
        
        fillComboBox();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btmRegistrar = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        lblCodigoAfiliado = new javax.swing.JLabel();
        txtCodigoAfiliad = new javax.swing.JTextField();
        lblNombreAfiliado = new javax.swing.JLabel();
        txtNombreAfiliado = new javax.swing.JTextField();
        lblApellidoAfiliado = new javax.swing.JLabel();
        txtApellidoAfiliado = new javax.swing.JTextField();
        cmbMaterial = new javax.swing.JComboBox();
        lblMaterial = new javax.swing.JLabel();
        lblEstado = new javax.swing.JLabel();
        cmbEstado = new javax.swing.JComboBox();
        btnNuevo = new javax.swing.JButton();
        lblTitulo = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        lblTitulo4 = new javax.swing.JLabel();
        lblMarcareg = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        btmRegistrar.setFont(new java.awt.Font("Tahoma", 3, 11)); // NOI18N
        btmRegistrar.setText("Registrar");
        btmRegistrar.setName("btmRegistrar"); // NOI18N
        btmRegistrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btmRegistrarActionPerformed(evt);
            }
        });

        btnCancelar.setFont(new java.awt.Font("Tahoma", 3, 11));
        btnCancelar.setText("Cancelar");
        btnCancelar.setName("btnCancelar"); // NOI18N
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jPanel1.setName("jPanel1"); // NOI18N

        lblCodigoAfiliado.setFont(new java.awt.Font("Tahoma", 2, 11)); // NOI18N
        lblCodigoAfiliado.setText("Cod. Afiliado :");
        lblCodigoAfiliado.setName("lblCodigoAfiliado"); // NOI18N

        txtCodigoAfiliad.setName("txtCodigoAfiliad"); // NOI18N
        txtCodigoAfiliad.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtCodigoAfiliadFocusLost(evt);
            }
        });

        lblNombreAfiliado.setFont(new java.awt.Font("Tahoma", 2, 11)); // NOI18N
        lblNombreAfiliado.setText("Nombre :");
        lblNombreAfiliado.setName("lblNombreAfiliado"); // NOI18N

        txtNombreAfiliado.setEditable(false);
        txtNombreAfiliado.setName("txtNombreAfiliado"); // NOI18N

        lblApellidoAfiliado.setFont(new java.awt.Font("Tahoma", 2, 11)); // NOI18N
        lblApellidoAfiliado.setText("Apellido :");
        lblApellidoAfiliado.setName("lblApellidoAfiliado"); // NOI18N

        txtApellidoAfiliado.setEditable(false);
        txtApellidoAfiliado.setName("txtApellidoAfiliado"); // NOI18N

        cmbMaterial.setName("cmbMaterial"); // NOI18N
        cmbMaterial.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cmbMaterialItemStateChanged(evt);
            }
        });

        lblMaterial.setFont(new java.awt.Font("Tahoma", 2, 11)); // NOI18N
        lblMaterial.setText("Material :");
        lblMaterial.setName("lblMaterial"); // NOI18N

        lblEstado.setFont(new java.awt.Font("Tahoma", 2, 11)); // NOI18N
        lblEstado.setText("Estado :");
        lblEstado.setName("lblEstado"); // NOI18N

        cmbEstado.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Pendiente", "Entregado" }));
        cmbEstado.setName("cmbEstado"); // NOI18N
        cmbEstado.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cmbEstadoItemStateChanged(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(lblMaterial)
                    .addComponent(lblCodigoAfiliado)
                    .addComponent(lblNombreAfiliado)
                    .addComponent(lblEstado))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(cmbMaterial, 0, 128, Short.MAX_VALUE)
                            .addComponent(txtNombreAfiliado, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtCodigoAfiliad, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(26, 26, 26)
                        .addComponent(lblApellidoAfiliado)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtApellidoAfiliado, javax.swing.GroupLayout.PREFERRED_SIZE, 112, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(29, 29, 29))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(cmbEstado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(26, 26, 26)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtCodigoAfiliad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblCodigoAfiliado))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(txtApellidoAfiliado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(lblApellidoAfiliado))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtNombreAfiliado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblNombreAfiliado))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblMaterial)
                            .addComponent(cmbMaterial, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblEstado)
                    .addComponent(cmbEstado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(71, 71, 71))
        );

        btnNuevo.setFont(new java.awt.Font("Tahoma", 3, 11)); // NOI18N
        btnNuevo.setText("Nuevo");
        btnNuevo.setName("btnNuevo"); // NOI18N
        btnNuevo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNuevoActionPerformed(evt);
            }
        });

        lblTitulo.setFont(new java.awt.Font("Times New Roman", 3, 18)); // NOI18N
        lblTitulo.setText("Comunidad Dinamica en Java");
        lblTitulo.setName("lblTitulo"); // NOI18N

        jLabel1.setBackground(new java.awt.Color(255, 51, 51));
        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/jacidco/resources/Logo_Jacidco_mini.png"))); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        lblTitulo4.setFont(new java.awt.Font("Times New Roman", 3, 36)); // NOI18N
        lblTitulo4.setText("Entrega Material");
        lblTitulo4.setName("lblTitulo4"); // NOI18N

        lblMarcareg.setFont(new java.awt.Font("Times New Roman", 0, 11)); // NOI18N
        lblMarcareg.setText("Copyright 2011. DCM. All Rights Reserved");
        lblMarcareg.setName("lblMarcareg"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel1)
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblTitulo, javax.swing.GroupLayout.PREFERRED_SIZE, 285, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblTitulo4)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(196, 196, 196)
                        .addComponent(btnNuevo)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btmRegistrar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnCancelar))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(lblMarcareg))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(21, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(lblTitulo)
                        .addGap(67, 67, 67)
                        .addComponent(lblTitulo4)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 244, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(11, 11, 11)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnCancelar)
                    .addComponent(btmRegistrar)
                    .addComponent(btnNuevo))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblMarcareg)
                .addContainerGap(26, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btmRegistrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btmRegistrarActionPerformed
        
        try
        {
            capturaDatos();
            guardaDatos();
        }
        catch(Exception ex)
        {
            Main.mensaje.errorGeneral(ex);
        }
        
    }//GEN-LAST:event_btmRegistrarActionPerformed

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        
        Main.goToMenuPrincipal();
        dispose();
    }//GEN-LAST:event_btnCancelarActionPerformed

    private void txtCodigoAfiliadFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtCodigoAfiliadFocusLost

        muestraNombreApellidoEmpleado();

    }//GEN-LAST:event_txtCodigoAfiliadFocusLost

    private void cmbMaterialItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cmbMaterialItemStateChanged
        
        

    }//GEN-LAST:event_cmbMaterialItemStateChanged

    private void cmbEstadoItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cmbEstadoItemStateChanged
        
}//GEN-LAST:event_cmbEstadoItemStateChanged

    private void btnNuevoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNuevoActionPerformed
        
        limpiarCampos();
    }//GEN-LAST:event_btnNuevoActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new EntregaMaterialFrame().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btmRegistrar;
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnNuevo;
    private javax.swing.JComboBox cmbEstado;
    private javax.swing.JComboBox cmbMaterial;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JLabel lblApellidoAfiliado;
    private javax.swing.JLabel lblCodigoAfiliado;
    private javax.swing.JLabel lblEstado;
    private javax.swing.JLabel lblMarcareg;
    private javax.swing.JLabel lblMaterial;
    private javax.swing.JLabel lblNombreAfiliado;
    private javax.swing.JLabel lblTitulo;
    private javax.swing.JLabel lblTitulo4;
    private javax.swing.JTextField txtApellidoAfiliado;
    private javax.swing.JTextField txtCodigoAfiliad;
    private javax.swing.JTextField txtNombreAfiliado;
    // End of variables declaration//GEN-END:variables

    public boolean capturaDatos()
    {
        this.codAfiliado = Integer.parseInt(txtCodigoAfiliad.getText());
        this.codMaterial = (cmbMaterial.getSelectedIndex()+1);
        this.estado = cmbMaterial.getItemAt(cmbMaterial.getSelectedIndex()).toString();
        return true;
    }

    public boolean guardaDatos() 
    {
        Main.bd.conectar();
        if(Main.bd.guardaDatosEntregaMaterial(codAfiliado, codMaterial, estado))
        {
            Main.mensaje.datosGuardaosOK("Datos de Entrega");
            txtCodigoAfiliad.setEnabled(false);
            cmbMaterial.setEnabled(false);
            cmbEstado.setEnabled(false);
        }
        Main.bd.cerrarConexion();
        return true;
    }

    public boolean limpiarCampos() 
    {
        txtCodigoAfiliad.setText("");
        txtNombreAfiliado.setText("");
        txtApellidoAfiliado.setText("");
        txtCodigoAfiliad.setEnabled(true);
        cmbMaterial.setEnabled(true);
        cmbEstado.setEnabled(true);
        return true;
    }
    
    /**
     * Llena el Combo Box con los nombres de los materiales disponibles
     */
    public void fillComboBox()
    {
        Main.bd.conectar();
        
        int cap = Main.bd.nextCode("cod_material","material")+1;
        
        Vector nombre = new Vector(cap);
        Vector nombre2 = new Vector(cap);
        
        nombre2 = Main.bd.muestraDatosComboBoxes(nombre,"material");
        
        for(int i=1;i<cap;i++)
        {
            cmbMaterial.addItem(nombre2.get(i));
        }
        
        Main.bd.cerrarConexion();
    }

    /**
     *
     */
    public void muestraNombreApellidoEmpleado()
    {
        int cod = Integer.parseInt(txtCodigoAfiliad.getText());

        Main.bd.conectar();
        ResultSet rs = Main.bd.buscaAfiliado(cod);

         try
        {
            if(rs.next())
            {
                txtNombreAfiliado.setText(rs.getString("primer_nombre"));
                txtApellidoAfiliado.setText(rs.getString("primer_apellido"));
            }
            else
            {
                Main.mensaje.advertencia("Afiliado no Existe");
            }
        }
        catch (SQLException ex)
        {

        }
        Main.bd.cerrarConexion();
    }
}
